import { AssetManager, JsonAsset, assetManager } from "cc";
import { Singleton } from "../Common/Singleton";

export class JsonManager extends Singleton {
    static get Ins() {
        return super.GetInstance<JsonManager>()
    }

    private bundleJson: AssetManager.Bundle = null

    getValue(name: string): any {
        return new Promise((resolve, reject) => {
            this.bundleJson.load(name, (err: any, res: JsonAsset) => {
                if (err) {
                    console.log('Bundle Json doesnt contain ' + name)
                } else {
                    resolve(res.json)
                }
            })
        })
    }

    getBundleJson(): Promise<any> {
        return new Promise((resolve, reject) => {
            assetManager.loadBundle('Json', (err, bundle) => {
                if (err) {
                    reject(err)
                    return
                }
                this.bundleJson = bundle
                resolve(true)
            });
        })
    }

    releaseAll() {
        this.bundleJson.releaseAll()
    }
}